/**
 * 
 */
package com.demo2do.mobile.web.controller.trading;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.Result;
import com.demo2do.core.utils.JsonUtils;

/**
 * @author downpour
 *
 */
@Controller
@RequestMapping("")
public class PaymentCallbackController {
    
    private static Logger logger = LoggerFactory.getLogger(PaymentCallbackController.class);
    
    /**
     * 
     * @param body
     * @param request
     * @return
     */
    @RequestMapping(value = "/wxpay/callback", method = RequestMethod.POST)
    public @ResponseBody String onPyamentCallback(@RequestBody String body,
                                                  HttpServletRequest request) {
        
        Map<String, Object> params = JsonUtils.parse(body);
        
        logger.trace("PaymentController#onSyncOrder - Get rquest number [{}], order number [{}], amount [{}], pay time [{}], channel [{}].", 
        		      params.get("requestNo"), params.get("orderNo"), params.get("transactionCode"), params.get("amount"), params.get("paymentTime"));
                                
        Result result = new Result();
        
        try {
            
            // check paymentRequest & signature
            
            
            // check order & update order, product things
            logger.trace("PaymentController#onSyncOrder() - Synchronizing order with result [{}] !!!!!!!!", result.getMessage());
            
        } catch (BusinessException e) {
            
            logger.error("PaymentController#onSyncOrder() - Synchronizing order encounters exception: {}", e);
            result.fail("500").message(e.getMessage());
            return JsonUtils.toJson(result);
            
        } catch (Exception e) {
            logger.error("PaymentController#onSyncOrder() - Synchronizing order encounters exception: {}", e);
            result.fail("500").message(e.getMessage());
            
        }
        
        return JsonUtils.toJson(result);
    }
    
}
